<?php
// Sankasya Model
// $Id: Sankasya.php 8 2009-12-29 12:13:12Z mocapapa@g.pugpug.org $
class Sankasya extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Sankasya':
	 * @var integer $id
	 * @var string $offkaicode
	 * @var string $sankasya
	 * @var string $password
	 * @var string $honbun
	 * @var string $modified
	 */

	public $authcode;
	public $dbpassword;
	const UNKOWN = '----';
	const PROTECT_WEEK = 2;
  
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'Sankasya';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			     array('offkaicode, sankasya, password, honbun', 'required', 'on'=>'insert, update'),
			     array('authcode', 'captcha', 'allowEmpty'=>!extension_loaded('gd'), 'on'=>'insert, update, delete'),
			     array('password', 'authenticate', 'on'=>'update, delete'),
			     array('sankasya', 'uniqueInOffkai', 'on'=>'insert'), // unique in offkai
			     array('sankasya', 'uniqueInOffkai2', 'on'=>'update'), // unique in offkai except myself
			     array('sankasya', 'finishCheck', 'on'=>'insert'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
			'offkai' => array(self::BELONGS_TO, 'Offkai', 'offkaicode'),
		);
	}

        /**
         * @return array customized attribute labels (name=>label)
         */
        public function attributeLabels()
        {
		return array(
			'offkaicode'=>'オフ会コード',
			'sankasya'=>'参加者',
			'password'=>'パスワード',
			'honbun'=>'一言',
			'modified'=>'修正日時',
			'authimage'=>'認証画像',
			'authcode'=>'認証コード',
		);
        }

        /**
         * Password validator
         */
        public function authenticate() {
		if ($this->password != $this->dbpassword) {
			$this->addError('password','パスワードが違います。');
		}
        }

        /**
         * Unique validator in a offkai
         */
        public function uniqueInOffkai() {
		// Yii::trace(get_class($this).'.uniqueInOffkai()');
		$criteria=new CDbCriteria;
		$criteria->condition = '`offkaicode` = '."'".$this->offkaicode."'";
		$criteria->condition .= ' AND `sankasya` = '."'".$this->sankasya."'";
		$sankasyaCount=Sankasya::model()->count($criteria->condition);
		if ($sankasyaCount) {
			$this->addError('sankasya','参加者 "'.$this->sankasya.'" は既にこのオフ会に参加しています。');
		}
        }

        /**
         * Unique validator in a offkai, other tan me
         */
        public function uniqueInOffkai2() {
		// Yii::trace(get_class($this).'.uniqueInOffkai()');
		$criteria=new CDbCriteria;
		$criteria->condition = '`offkaicode` = '."'".$this->offkaicode."'";
		$criteria->condition .= ' AND `sankasya` = '."'".$this->sankasya."'";
		$criteria->condition .= ' AND `id` <> '."'".$_GET['id']."'";
		$sankasyaCount=Sankasya::model()->count($criteria->condition);
		if ($sankasyaCount) {
			$this->addError('sankasya','参加者 "'.$this->sankasya.'" は既にこのオフ会に参加しているようです。');
		}
        }


        /**
         * Finish check validator
         */
        public function finishCheck() {
		// Yii::trace(get_class($this).'.finishCheck()');
		if ($this->offkai->joutai == '定員になりました' || $this->offkai->joutai == '終了しました')
			$this->addError('sankasya','このオフ会「'.$this->offkai->meishou.'」は'.$this->offkai->joutai.'。');
        }

	public function behaviors(){
		return array(
		       'AutoTimestampBehavior' => array(
				'class' => 'application.components.AutoTimestampBehavior',
			)
		);
	}
}